import Vue from 'vue'
import Router from 'vue-router'


Vue.use(Router)

const router = new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/',
      redirect: "/user",
      component: () => import(/* webpackChunkName: "about" */ './views/main.vue'),
      children:[
        {
          path: '/user',
          name: 'user',
          component: () => import(/* webpackChunkName: "about" */ './views/user/userManagment.vue')
        }
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: () => import(/* webpackChunkName: "about" */ './views/login.vue')
    },
    {
      path: '/reg',
      name: 'reg',
      component: () => import(/* webpackChunkName: "about" */ './views/register.vue')
    }
  ]
})
router.beforeEach((to, from, next) => {
  if (to.path === '/login'||to.path === '/reg') {
    next()
    return
  }
  const token = window.sessionStorage.getItem('user')
  //没有登录访问
  if (!token) {

    next('/login')
  }
  //有登录
  next();
})
export default router
